
local excel = load_excel('特殊奖池.xlsx','装备宝箱')
local colors = {
    0xff00fff00,
    0xff00ccff,
    0xffca42fd,
}
ac.game:event '单位-初始化'(function(_,unit)
    local name = unit.name
    if name=='宝箱' then
        local lv = math.random(1,3)
        unit:add_type('建筑')
        unit:add('生命加成',(lv-1)*100)
        unit['宝箱品质'] = lv
        unit:set_color(colors[lv])
        unit:play_animation('birth')
    end
    if name=='装备宝箱' or name=='山丘之王' then
        local lv = ac.enemy and ac.enemy.boss_index or 0
        local weight = {}
        for a,da in ipairs(excel) do
            local odds = da['权重'] + (da['提升'] or 0) * lv
            weight[#weight + 1] = {a,odds}
        end
        unit:add_type('建筑')
        unit['装备权重'] = weight
    end
end)

local jl = {
    {'金币',20,100},
    {'杀敌',1,10},
    {'木材',1,5},
}

ac.game:event '玩家-宝箱奖励'(function(_,player)
    local da = table.random(jl)
    local name = da[1]
    local num = math.random(da[2],da[3])
    player:add_icon(name,num)
end)

ac.game:event '玩家-创建Boss宝箱'(function(_,player,point)
    local unit = ac.player[12]:create_unit('Boss宝箱',point)
    unit:set_size(1.5)
    unit.title = ('%s|nBoss宝箱'):format(player:get_color_name())
    unit:add_buff '挑战限时'{time = 20}
    unit:set('生命上限',999)
    unit:set('移动速度',0)
    unit_add_life2(unit)
    unit:event '单位-伤害结算前'(function(_,_,target,data)
        data.current_damage = 1
        player:notify('玩家-宝箱奖励',player)
    end)
end)

ac.game:event '英雄-击杀单位'(function(_,hero,unit)
    local name = unit.name
    local player = hero.owner
    if name=='宝箱' then
        local jl = table.random{'金币','木材','杀敌'}
        local lv = unit['宝箱品质'] or 1
        if jl=='金币' then
            local ex = math.floor(ac.enemy:get_time()/1000)
            local num = math.random(500*lv + ex,1000*lv + ex)
            player:add_icon('金币',num,unit)
        elseif jl=='木材' then
            local num = 50 * lv
            player:add_icon('木材',num,unit)
        elseif jl=='杀敌' then
            local ex = math.floor(ac.enemy:get_time()/1000 * 0.2)
            local num = math.random(100*lv + ex,200*lv + ex)
            player:add_icon('杀敌',num,unit)
        end
    end
    local weight = unit['装备权重']
    if weight then
        local num = math.random(3,5)
        for a=1,num do
            local lv = table.get_by_weight(weight)
            local name = random_item_name(lv)
            local item = collate_item(hero,name)
            player:send_msg(('获得:%s'):format(item:get_title()))
        end
    end
end)